#!/usr/bin/python3
#coding:utf-8

import os
import sys
import xlsxwriter
import time
import re


# 获取平台编码格式
#print (sys.getdefaultencoding())

# 获取当前路径
this_path = os.getcwd()
print(this_path)

# 等待执行 回车继续执行
x = input("Enter <-- :")				# pyrhon3


def open_File(path):
	# 打开文件目录

	# 读取文件目录
	dir_lists = os.listdir(path)
	##print (dir_lists)
	##print (dir_lists[0])

	dir_list = []
	for item in dir_lists:
		##print (item)
		# 读取后缀名为 DAT 的文件，追加  文件列表中
		suffix = item[-3:]
		##print (suffix)
		if suffix == 'txt':
			dir_list.append(item)
	# print (dir_list)
	
	return dir_list

path = "./DATA"
dir_list = open_File(path)	





	
start = time.clock()	# 计算程序执行时间
# 循环 读取所有文件
for file in dir_list:
	##print (file)
	path = file

	fp = open("DATA/" + path,'rb')	# 打开文件

	
	# 拼装 excel文件名
	workbook_name = path[:-3]		# 截取文件名称
	##print (workbook_name)
	workbook_name = "DATA/" + workbook_name + ".xlsx"	# 拼装 excel文件名
	#print (workbook_name)
	workbook	= xlsxwriter.Workbook(workbook_name)	# excel文件名
	
	worksheet1 	= workbook.add_worksheet('策略表')		# 表单 1
	worksheet2 	= workbook.add_worksheet('nat')			# 表单 1
	worksheet3 	= workbook.add_worksheet('route')		# 表单 1

	# 总样式
	format_1=workbook.add_format()    	#定义format_1格式对象
	format_1.set_align('center')		#format_1对象单元格居中对齐的格式

	# worksheet 1 (interface) 接口配置列表 基础设置
	worksheet1.write('A1', '序号', format_1)
	worksheet1.write('B1', '名称', format_1)
	worksheet1.write('C1', '动作', format_1)
	worksheet1.write('D1', '协议', format_1)
	worksheet1.write('E1', '源地址', format_1)
	worksheet1.write('F1', '掩码', format_1)
	worksheet1.write('G1', '目的地址', format_1)
	worksheet1.write('H1', '掩码', format_1)
	worksheet1.write('I1', '服务', format_1)
	
	# worksheet 2 (interface) 接口配置列表 基础设置
	worksheet2.write('A1', '序号', format_1)
	worksheet2.write('B1', 'nat', format_1)
	worksheet2.write('C1', 'inside', format_1)
	worksheet2.write('D1', 'ID', format_1)
	worksheet2.write('E1', 'ip', format_1)
	worksheet2.write('F1', '掩码', format_1)

	# worksheet 3 (interface) 接口配置列表 基础设置
	worksheet3.write('A1', '序号', format_1)
	worksheet3.write('B1', 'rout', format_1)
	worksheet3.write('C1', 'outside', format_1)
	worksheet3.write('D1', 'ip', format_1)
	worksheet3.write('E1', '掩码', format_1)
	worksheet3.write('F1', 'ip', format_1)
	worksheet3.write('G1', 'ID', format_1)
	
	
	# worksheet 1 设置样式
	worksheet1.set_column('B:B', 15)    #设定第一列（A）宽度为20像素
	worksheet1.set_column('D:D', 20)    #设定第一列（A）宽度为20像素
	worksheet1.set_column('E:E', 20)    #设定第一列（A）宽度为20像素
	
	# worksheet1（rule_id）表单计数
	rule_id_number = 0
	rule_id_B = 1
	rule_id_C = 1
	rule_id_D = 1
	rule_id_E = 1
	rule_id_F = 1
	rule_id_G = 1
	rule_id_H = 1
	rule_id_I = 1
	
	# worksheet2（nat）表单计数
	nat_number = 0
	nat_id_B = 1
	nat_id_C = 1
	nat_id_D = 1
	nat_id_E = 1
	nat_id_F = 1
	
	# worksheet3（route）表单计数
	route_number = 0
	route_id_B = 1
	route_id_C = 1
	route_id_D = 1
	route_id_E = 1
	route_id_F = 1
	route_id_G = 1

######################################################################################	
	# 统计各种数据
	line_num = 0
	access_list_nums = 0
	access_list_9 = 0
	access_list_11 = 0
	nat = 0
	
	line = fp.readline().decode('utf-8')	# 判断文件是否读完
	while line:
		#readline([size])        # 读一行，如果定义了size，有可能返回的只是一行的一部分
		line = fp.readline().decode('utf-8')
		# print(line)
		
		valueList = line.split()	# 将字符串 转为 列表
		# print (valueList)
		
		line_num += 1		#行号计数器
		#print(line_num,'---',valueList)
		
		# 所有操作都在 非空 情况下操作
		if len(valueList) != 0 : # 判断是否为空
			# print(valueList)
			# print(len(valueList))
			
			# 计算（access-list 总数）
			if (valueList[0] == 'access-list'):
				access_list_nums += 1
				
			if (valueList[0] == 'access-list' and len(valueList)==9):
				access_list_9 += 1
			
			if (valueList[0] == 'access-list' and len(valueList)==11):
				# print(valueList)
				access_list_11 += 1
			
			if (valueList[0] == 'access-list' and len(valueList)!=11 and len(valueList)!=9):
				#print(valueList)
				#print(len(valueList))
				pass
			
			
			if (valueList[0]=="nat"):
				nat += 1
				
			
	#print('line_nums: ',line_num)
	#print("access-list_nums: ", access_list_nums)
	#print("access_list_9: ", access_list_9)
	#print("access_list_11: ", access_list_11)
	
	print(nat)
	
	fp.close()
	
	
#########################################################################################	
	# 文件写入操作
	line_num = 1
	
	file_name = []
	
	
	fp = open("DATA/" + path,'rb')	# 打开文件
	line = fp.readline().decode('utf-8')	# 判断文件是否读完
	while line:
		#readline([size])        # 读一行，如果定义了size，有可能返回的只是一行的一部分
		line = fp.readline().decode('utf-8')
		# print(line)
		
		valueList = line.split()	# 将字符串 转为 列表
		# print (valueList)
		
		line_num += 1		#行号计数器
		#print(line_num)
		
		
		# 所有操作都在 非空 情况下操作
		if len(valueList) != 0 : # 判断是否为空
			# print(valueList)
			# print(len(valueList))
			# print(len(valueList),'---',line_num)
			
			
			
			
			# 列表长度为 9 的数据
			if (valueList[0] == 'access-list' and len(valueList)==9):
				#print(valueList)
				#valueList = [' ' if x == 'host' else x for x in valueList]

				rule_id_number = rule_id_number + 1
				rule_id_B = rule_id_B + 1
				rule_id_C = rule_id_C + 1
				rule_id_D = rule_id_D + 1
				rule_id_E = rule_id_E + 1
				rule_id_F = rule_id_F + 1
				rule_id_G = rule_id_G + 1
				rule_id_H = rule_id_H + 1
				rule_id_I = rule_id_I + 1				
				
				if valueList[5] == "host":
					valueList_ydz = valueList[6]
					worksheet1.write('E'+str(rule_id_E), valueList_ydz, format_1)	#源地址
					
				else:
					worksheet1.write('E'+str(rule_id_E), valueList[5], format_1)	#源地址
					worksheet1.write('F'+str(rule_id_F), valueList[6], format_1)	#
				
				if valueList[7] == "host":
					valueList_mddz = valueList[8]
					worksheet1.write('G'+str(rule_id_G), valueList_mddz, format_1)	#目的地址
				else:
					worksheet1.write('G'+str(rule_id_G), valueList[7], format_1)	#目的地址
					worksheet1.write('H'+str(rule_id_H), valueList[8], format_1)	#

				
				worksheet1.write('A'+str(rule_id_number + 1), rule_id_number, format_1)	#序号
				worksheet1.write('B'+str(rule_id_B), valueList[1], format_1)	#ID
				worksheet1.write('C'+str(rule_id_C), valueList[3], format_1)	#ID
				worksheet1.write('D'+str(rule_id_D), valueList[4], format_1)	#ID


			if (valueList[0] == 'access-list' and len(valueList)==11):
				#valueList = [' ' if x == 'host' else x for x in valueList]
			
				rule_id_number = rule_id_number + 1
				rule_id_B = rule_id_B + 1
				rule_id_C = rule_id_C + 1
				rule_id_D = rule_id_D + 1
				rule_id_E = rule_id_E + 1
				rule_id_F = rule_id_F + 1
				rule_id_G = rule_id_G + 1
				rule_id_H = rule_id_H + 1
				rule_id_I = rule_id_I + 1
				
				
				
				if valueList[5] == "host":
					valueList_ydz = valueList[6]
					worksheet1.write('E'+str(rule_id_E), valueList_ydz, format_1)	#源地址
					
				else:
					worksheet1.write('E'+str(rule_id_E), valueList[5], format_1)	#源地址
					worksheet1.write('F'+str(rule_id_F), valueList[6], format_1)	#
				
				if valueList[7] == "host":
					valueList_mddz = valueList[8]
					worksheet1.write('G'+str(rule_id_G), valueList_mddz, format_1)	#目的地址
				else:
					worksheet1.write('G'+str(rule_id_G), valueList[7], format_1)	#目的地址
					worksheet1.write('H'+str(rule_id_H), valueList[8], format_1)	#
				

				worksheet1.write('A'+str(rule_id_number + 1), rule_id_number, format_1)	#序号
				worksheet1.write('B'+str(rule_id_B), valueList[1], format_1)	#ID
				worksheet1.write('C'+str(rule_id_C), valueList[3], format_1)	#ID
				worksheet1.write('D'+str(rule_id_D), valueList[4], format_1)	#ID
				worksheet1.write('I'+str(rule_id_I), valueList[10], format_1)	#ID					
				
			if (valueList[0] == 'access-list' and len(valueList)==12):	
				#valueList = [' ' if x == 'host' else x for x in valueList]
			
				rule_id_number = rule_id_number + 1
				rule_id_B = rule_id_B + 1
				rule_id_C = rule_id_C + 1
				rule_id_D = rule_id_D + 1
				rule_id_E = rule_id_E + 1
				rule_id_F = rule_id_F + 1
				rule_id_G = rule_id_G + 1
				rule_id_H = rule_id_H + 1
				rule_id_I = rule_id_I + 1
				
				if valueList[5] == "host":
					valueList_ydz = valueList[6]
					worksheet1.write('E'+str(rule_id_E), valueList_ydz, format_1)	#源地址
					
				else:
					worksheet1.write('E'+str(rule_id_E), valueList[5], format_1)	#源地址
					worksheet1.write('F'+str(rule_id_F), valueList[6], format_1)	#
				
				if valueList[7] == "host":
					valueList_mddz = valueList[8]
					worksheet1.write('G'+str(rule_id_G), valueList_mddz, format_1)	#目的地址
				else:
					worksheet1.write('G'+str(rule_id_G), valueList[7], format_1)	#目的地址
					worksheet1.write('H'+str(rule_id_H), valueList[8], format_1)	#
				

				worksheet1.write('A'+str(rule_id_number + 1), rule_id_number, format_1)	#序号
				worksheet1.write('B'+str(rule_id_B), valueList[1], format_1)	#ID
				worksheet1.write('C'+str(rule_id_C), valueList[3], format_1)	#ID
				worksheet1.write('D'+str(rule_id_D), valueList[4], format_1)	#ID
				
				valueList_slip = valueList[-3:]
				valueList_slip = ' '.join(valueList_slip)
				worksheet1.write('I'+str(rule_id_I), valueList_slip, format_1)	#ID		

			if (valueList[0] == 'access-list' and len(valueList)==7):	
				#valueList = [' ' if x == 'host' else x for x in valueList]
			
				rule_id_number = rule_id_number + 1
				rule_id_B = rule_id_B + 1
				rule_id_C = rule_id_C + 1
				rule_id_D = rule_id_D + 1
				rule_id_E = rule_id_E + 1
				rule_id_F = rule_id_F + 1
				rule_id_G = rule_id_G + 1
				rule_id_H = rule_id_H + 1
				rule_id_I = rule_id_I + 1

				worksheet1.write('A'+str(rule_id_number + 1), rule_id_number, format_1)	#序号
				worksheet1.write('B'+str(rule_id_B), valueList[1], format_1)	#ID
				worksheet1.write('C'+str(rule_id_C), valueList[3], format_1)	#ID
				worksheet1.write('D'+str(rule_id_D), valueList[4], format_1)	#ID
				worksheet1.write('E'+str(rule_id_E), valueList[5], format_1)	#ID
				worksheet1.write('G'+str(rule_id_G), valueList[6], format_1)	#ID

			
			if (valueList[0]=="nat"):
				#print(valueList)
				nat_number = nat_number + 1
				nat_id_B = nat_id_B + 1
				nat_id_C = nat_id_C + 1
				nat_id_D = nat_id_D + 1
				nat_id_E = nat_id_E + 1
				nat_id_F = nat_id_F + 1

				worksheet2.write('A'+str(nat_number + 1), nat_number, format_1)	#序号
				worksheet2.write('B'+str(nat_id_B), valueList[0], format_1)	#ID
				worksheet2.write('C'+str(nat_id_C), valueList[1], format_1)	#ID
				worksheet2.write('D'+str(nat_id_D), valueList[2], format_1)	#ID
				worksheet2.write('E'+str(nat_id_E), valueList[3], format_1)	#ID
				worksheet2.write('F'+str(nat_id_F), valueList[4], format_1)	#ID
				
			if (valueList[0]=="route"):
				route_number = route_number + 1
				route_id_B = route_id_B + 1
				route_id_C = route_id_C + 1
				route_id_D = route_id_D + 1
				route_id_E = route_id_E + 1
				route_id_F = route_id_F + 1
				route_id_G = route_id_G + 1

				worksheet3.write('A'+str(route_number + 1), route_number, format_1)	#序号
				worksheet3.write('B'+str(route_id_B), valueList[0], format_1)	#ID
				worksheet3.write('C'+str(route_id_C), valueList[1], format_1)	#ID
				worksheet3.write('D'+str(route_id_D), valueList[2], format_1)	#ID
				worksheet3.write('E'+str(route_id_E), valueList[3], format_1)	#ID
				worksheet3.write('F'+str(route_id_F), valueList[4], format_1)	#ID
				worksheet3.write('G'+str(route_id_G), valueList[5], format_1)	#ID
								

	
	fp.close()				# 关闭文件
	workbook.close()		# 关闭excel



print("OK !!!!!!")
end = time.clock()
print("run time : %f s" % (end - start))

input()
